package android.money;

import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.money.data.Account;
import android.money.data.Card;
import android.money.data.Transaction;
import android.money.db.AccountManager;
import android.money.db.DBInfo;
import android.money.db.TransactionManager;
import android.money.network.SyncConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TransactionConfirm extends Activity implements OnClickListener {

	private boolean actionDone = false;

	private String payingText = "Paying...";

	private String TXT_PAY_SUCCESS = "Paid!";

	private String TXT_PAY_FAIL = "Pay Failed";

	private TextView readerName;

	private TextView amountText;

	private TextView unitText;

	private Spinner cardSpinner;

	private Button payButton;

	private AccountManager accManager;

	private Cursor accountCursor;

	public static final int PAY_SUCCESS = 0x001;

	public static final int PAY_FAIL = 0x002;

	public Handler payHandler = new Handler() {
		// @Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case PAY_SUCCESS:
				Toast.makeText(TransactionConfirm.this, TXT_PAY_SUCCESS,
						Toast.LENGTH_LONG).show();

				TransactionManager transManager = new TransactionManager(
						TransactionConfirm.this).open();
				Transaction transaction = new Transaction();
				transaction.setAccount_id(accountCursor.getInt(accountCursor
						.getColumnIndexOrThrow(DBInfo.account_id)));
				transaction.setAmount(Double.parseDouble(amountText.getText()
						.toString()));
				transaction.setReader(readerName.getText().toString());
				transaction.setUnit(unitText.getText().toString());
				transaction.setTime(new Date().getTime());

				transManager.addTransaction(transaction);
				break;
			case PAY_FAIL:
				Toast.makeText(TransactionConfirm.this, TXT_PAY_FAIL,
						Toast.LENGTH_LONG).show();
				break;
			}
			actionDone = true;
			finish();
		}
	};

	public Account getPayAccount() {
		Account account = new Account();
		account.setNumber(accountCursor.getString(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_number)));
		account.setType(new Card(accountCursor.getInt(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_type))));
		account.setSec_code((accountCursor.getString(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_sec_code))));
		account.setHolderName(accountCursor.getString(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_holder_name)));
		account.setExpire_month(accountCursor.getShort(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_expire_month)));
		account.setExpire_year(accountCursor.getShort(accountCursor
				.getColumnIndexOrThrow(DBInfo.account_expire_year)));
		return account;
	}

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.transaction_confirm_page);
		accManager = new AccountManager(this);
		accManager.open();

		readerName = (TextView) findViewById(R.id.readerHostName);
		readerName.setText(getIntent().getExtras().getString(
				DBInfo.trans_reader));
		amountText = (TextView) findViewById(R.id.amount);
		amountText.setText(getIntent().getExtras().getString(
				DBInfo.trans_amount)
				+ "");
		unitText = (TextView) findViewById(R.id.unit);
		unitText.setText(getIntent().getExtras().getString(DBInfo.trans_unit)
				+ "");
		cardSpinner = (Spinner) findViewById(R.id.cardSpinner);
		accountCursor = accManager.fetchAllAccounts();
		startManagingCursor(accountCursor);
		SpinnerAdapter cardAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_dropdown_item,
				accountCursor, new String[] { DBInfo.account_name },
				new int[] { android.R.id.text1 });
		cardSpinner.setAdapter(cardAdapter);
		payButton = (Button) findViewById(R.id.payButton);
		payButton.setOnClickListener(this);

	}

	public void onClick(View arg0) {
		payButton.setText(payingText);
		payButton.setEnabled(false);
		Sync.syncRunnable.setTransactionConfirm(this);
		Sync.syncRunnable.setStartPay();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (!actionDone) {
			Sync.syncRunnable.disconnectToPos();
			Toast.makeText(TransactionConfirm.this, "DISCONNECTED",
					Toast.LENGTH_LONG).show();
		}
	}
}
